<?php
App::uses('AppController', 'Controller');
/**
 * Systems Controller
 *
 * @property System $System
 * @property PaginatorComponent $Paginator
 */
class SystemsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->System->recursive = 0;
		$this->set('systems', $this->Paginator->paginate());
		$this->set('title_layout', 'Hệ điều hành.');
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->System->exists($id)) {
			throw new NotFoundException(__('Invalid system'));
		}
		$options = array('conditions' => array('System.' . $this->System->primaryKey => $id));
		$this->set('system', $this->System->find('first', $options));
		$this->set('title_layout', 'Xem thông tin hệ điều hành');
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->System->create();
			$this->request->data['System']['alias'] = $this->get_alias($this->request->data['System']['name']);
			if ($this->System->save($this->request->data)) {
				$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin hệ điều hành thành công.', 'default', array('class' => 'alert alert-success'), 'good');
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin hệ điều hành thất bại.', 'default', array('class' => 'alert alert-error'), 'bad');
			}
		}
		$games = $this->System->Game->find('list');
		$this->set(compact('games'));
		$this->set('title_layout', 'Thêm hệ điều hành mới');
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->System->exists($id)) {
			throw new NotFoundException(__('Invalid system'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['System']['alias'] = $this->get_alias($this->request->data['System']['name']);
			if ($this->System->save($this->request->data)) {
				$this->Session->setFlash(__('The system has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The system could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('System.' . $this->System->primaryKey => $id));
			$this->request->data = $this->System->find('first', $options);
		}
		$games = $this->System->Game->find('list');
		$this->set(compact('games'));
		$this->set('title', 'Sửa thông tin hệ điều hành');
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->System->id = $id;
		if (!$this->System->exists()) {
			throw new NotFoundException(__('Invalid system'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->System->delete()) {
			$this->Session->setFlash(__('The system has been deleted.'));
		} else {
			$this->Session->setFlash(__('The system could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
	
	
	public function menu() {
		$menus = $this->System->find('all', 
			array(
				'recursive' => -1,
			)
		);
		return $menus;
	}
}
